package cc.rengu.igas.route.common.dao.impl;

import cc.rengu.igas.route.common.dao.WeightCfgMapper;
import cc.rengu.igas.route.common.entity.WeightCfg;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 智能路由权重配置表操作类型
 *
 * @author xlz
 * @version 1.0
 * @date 2020-04-05
 */
public class WeightCfgMapperImpl implements WeightCfgMapper {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.ROUTE_POOL_NAME);

    @Override
    public WeightCfg getWeightCfgList(String instId, int ruleId) throws Exception {
        int iReturnCode;
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        if (null == dbsUtil) {
            throw new Exception("获取数据库连接失败");
        }
        String sqlStr = (String) UnifiedCache.get(
                AppParamConstant.SQL_MAPPER_CACHE, "getListWeightCfg");
        if (StringUtil.isNullorEmpty(sqlStr)) {
            throw new Exception("获取数据操作SQL失败");
        }

        WeightCfg weightCfg = new WeightCfg();
        weightCfg.setInstId(instId);
        weightCfg.setRuleId(ruleId);

        iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, weightCfg, weightCfg);

        if (Database.DBS_SUCCESS != iReturnCode &&
                Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("获取智能路由权重配置表失败,iReturnCode:<{}>", iReturnCode);
            throw new Exception("获取智能路由权重配置表失败");
        } else if (Database.DBS_NOT_FOUND == iReturnCode) {
            rglog.error("无数据");
            return null;
        }
        return weightCfg;
    }
}
